package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.AccountBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 账号
 */
public class AccountActivity extends BaseActivity implements View.OnClickListener, DialogUtil.DialogClickListener, PlatformActionListener {
    private static final int REQUEST_CHANGE_CODE = 8877;
    private static final int REQUEST_BIND_PHONE_CODE = 6985;
    private TextView tvPhoneTitle,tvPhone, tvBindTitle;//手机号
    private RelativeLayout rlPhone, rlWechat, rlQQ, rlSina;//手机号，微信，QQ,新浪
    private TextView tvWechat, tvQQ, tvSina;//是否绑定显示
    private int bindPhone, bindWechat, bindQQ, bindSina;//1绑定手机号，1绑定微信，1绑定QQ，1绑定微博
    private int unBindType;//解绑哪个2qq,1微信，3新浪
    private String mPhone,mQQId,mWechatId,mSinaId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        setTitle("账号", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        mQQId = MyApplication.getInstance().sputil.getString(ConstantUserInfo.QQ_ID);//qq
        mWechatId = MyApplication.getInstance().sputil.getString(ConstantUserInfo.WECHAT_ID);//wechat
        mSinaId = MyApplication.getInstance().sputil.getString(ConstantUserInfo.SINA_ID);//sina
        mPhone = MyApplication.getInstance().sputil.getString(ConstantUserInfo.MOBILE);

        initView();
        initListener();

        setAccountBindState();

//        loadWaitDialog();
//        HttpMethod.myNumber(this, this);
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        tvPhoneTitle = (TextView) findViewById(R.id.textView4);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvBindTitle = (TextView) findViewById(R.id.tv_bind_title);
        rlPhone = (RelativeLayout) findViewById(R.id.rl_phone);
        rlWechat = (RelativeLayout) findViewById(R.id.rl_wechat);
        rlQQ = (RelativeLayout) findViewById(R.id.rl_qq);
        rlSina = (RelativeLayout) findViewById(R.id.rl_sina);
        tvWechat = (TextView) findViewById(R.id.tv_wechat_bind);
        tvQQ = (TextView) findViewById(R.id.tv_qq_bind);
        tvSina = (TextView) findViewById(R.id.tv_sina_bind);

    }

    private void setAccountBindState(){
        if(!StringUtils.isEmpty(mPhone)){
            bindPhone = 1;
            tvBindTitle.setText("更换手机号");
        }else{
            bindPhone = 0;
            tvBindTitle.setText("绑定手机号");
            tvPhoneTitle.setText("未绑定手机号");
        }
        if(!StringUtils.isEmpty(mQQId)){
            tvQQ.setText("已经绑定");
            bindQQ = 1;
        }else{
            tvQQ.setText("暂未绑定");
            bindQQ = 0;
        }
        if(!StringUtils.isEmpty(mWechatId)){
            tvWechat.setText("已经绑定");
            bindWechat = 1;
        }else{
            tvWechat.setText("暂未绑定");
            bindWechat = 0;
        }
        if(!StringUtils.isEmpty(mSinaId)){
            tvSina.setText("已经绑定");
            bindSina= 1;
        }else{
            tvSina.setText("暂未绑定");
            bindSina= 0;
        }
    }

    private void initListener() {
        rlPhone.setOnClickListener(this);
        rlWechat.setOnClickListener(this);
        rlQQ.setOnClickListener(this);
        rlSina.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_phone://手机号
                if (bindPhone == 1) {//如果绑定手机号,修改
                    startActivity(ChangePhoneActivity.class);
                } else {//如果未绑定手机号
                    startActivityForResult(BindPhoneActivity.class, new Bundle(), REQUEST_BIND_PHONE_CODE);
                }
                break;
            case R.id.rl_wechat://微信
                if (bindWechat == 1) {
                    DialogUtil.showRelieveBindDialog(this, 1, this).show();
                } else {
                    Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                    authorize(wechat);
                    break;
                }
                break;
            case R.id.rl_qq://QQ
                if (bindQQ == 1) {
                    DialogUtil.showRelieveBindDialog(this, 2, this).show();
                } else {
                    Platform qq = ShareSDK.getPlatform(QQ.NAME);
                    authorize(qq);
                }
                break;
            case R.id.rl_sina://新浪
                if (bindSina == 1) {
                    DialogUtil.showRelieveBindDialog(this, 3, this).show();
                } else {
                    Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
                    authorize(sina);
                }
                break;
        }
    }

    private void authorize(Platform plat) {
        if (plat == null) {
            return;
        }
//        //判断指定平台是否已经完成授权
//        if (plat.isAuthValid()) {
//            String userId = plat.getDb().getUserId();
//            if (userId != null) {
//
//                return;
//            }
//        }
//        if (plat.isAuthValid()) {
//            plat.removeAccount(true);
//            return;
//        }
        plat.setPlatformActionListener(this);
        // true不使用SSO授权，false使用SSO授权
        plat.SSOSetting(false);
//        //获取用户资料
        plat.showUser(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setPhone();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            bindPhone = 1;
        }
    }

    private void setPhone() {
        mPhone = MyApplication.getInstance().sputil.getString(ConstantUserInfo.MOBILE);
        if (!StringUtils.isEmpty(mPhone) && mPhone.length() > 7) {
            mPhone = mPhone.substring(0, 3) + "****" + mPhone.substring(7, 11);
            tvPhone.setText(mPhone);
            tvBindTitle.setText("更换手机号");
            tvPhoneTitle.setText("当前手机号");
        }
    }

    @Override
    public void dialogClick(int type) {
        unBindType = type;
        switch (type) {
            case 1://微信
                unBind("1");
                break;
            case 2://QQ
                unBind("2");
                break;
            case 3://新浪
                unBind("3");
                break;
        }
    }

    /**
     * 8.20解绑微信 qq 微博
     *
     * @param type  1 微信 2 qq 3 微博
     */
    private void unBind(String type) {
        loadWaitDialog();
        HttpMethod.unBundle(this, this, type);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_UNBUNDLE) {
            ToastUtil.makeLongText(this, "解绑成功");
            switch (unBindType){
                case 1://微信
                    mWechatId=null;
                    break;
                case 2://QQ
                    mQQId  = null;
                    break;
                case 3://新浪
                   mSinaId = null;
                break;
            }
            setAccountBindState();
            saveBindState();
        }else if (method == HttpMethod.HTTP_USER_BUNDLE) {
            setAccountBindState();
            saveBindState();
        }
    }

    private void saveBindState(){
        MyApplication.getInstance().sputil.put(ConstantUserInfo.QQ_ID,mQQId);//qq
        MyApplication.getInstance().sputil.put(ConstantUserInfo.WECHAT_ID, mWechatId);//wechat
        MyApplication.getInstance().sputil.put(ConstantUserInfo.SINA_ID, mSinaId);//sina
    }

    @Override
    protected void onhttpFailed(String message, int method) {
//        super.onhttpFailed(message, method);
        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(this, message);
        }
        dismissWaitDialog();
    }

    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
        if (action == Platform.ACTION_USER_INFOR) {
            PlatformDb platDB = platform.getDb();//获取数平台数据DB
            //通过DB获取各种数据
//            platDB.getToken();
            String pic = platDB.getUserIcon();
            String wid = platDB.getUserId();
            String userName = platDB.getUserName();
//                userName = platDB.getUserGender();//性別

            int type = 0;
            if (Wechat.NAME.equals(platform.getName())) {
                mWechatId = wid;
                type = 1;
                bindThirdParty(type,pic, wid, userName);
            } else if (QQ.NAME.equals(platform.getName())) {
                mQQId = wid;
                type = 2;
                bindThirdParty(type,pic, wid, userName);
            } else if (SinaWeibo.NAME.equals(platform.getName())) {
                mSinaId = wid;
                type = 3;
                bindThirdParty(type,pic, wid, userName);
            }
        }
    }


    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        //授权失败
        ToastUtil.makeLongText(this, "授权失败");
    }

    @Override
    public void onCancel(Platform platform, int i) {
        //取消授权
    }

    private void bindThirdParty(int type, String pic, String wid, String userName) {
        HttpMethod.bindMobile(this, this, String.valueOf(type), userName, pic, wid);
    }
}
